package com.air.ota.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;
import com.air.ota.mo.InboundMsg;
import com.air.ota.mo.RegTrans;
import com.air.ota.service.IRegResultHandlerService;
import com.air.ota.service.IRegTransService;
import com.air.ota.service.ISMGPService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.SZTInterface;
import com.air.ota.vo.SZTBaseResponseVO;
import com.air.ota.vo.SZTRegResultReqTransVO;
import com.air.ota.vo.SZTRegResultRequestVO;
import com.air.ota.vo.SZTRegResultResTransVO;
import com.air.ota.vo.UTKRegResultUplinkVO;

@Service("regResultHandlerService")
public class RegResultHandlerServiceImp extends BaseUplinkMsgHandler
	implements IRegResultHandlerService {
	
	@Autowired
	IRegTransService regTransService;
	@Autowired
	ISMGPService smgpService;
	
	@Override
	public void process(UTKRegResultUplinkVO pkgVO, InboundMsg inboundMsg) throws Exception {
		System.out.println("------------Ready to Process Reg Result");
		
		
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("regSequence",HexStringUtil.bytesToHexString(pkgVO.getRegSequence()) , QueryExpression.EQUALS);
		condition.addQueryCondition("cellphone",inboundMsg.getPhoneNumber() , QueryExpression.EQUALS);
		List<RegTrans> result= regTransService.queryByCondition(condition);
		RegTrans regTrans = result.get(0);
		
		regTrans.setResult(AppConstant._transResult_success_all);
		
		regTransService.updateById(regTrans);
		unlockRetryOutboundMsg(regTrans.getId(),AppConstant._outbound_msg_type_regResponse);
		
		
		if (!("9000").equals(HexStringUtil.bytesToHexString(pkgVO.getRegResult()))){
			regTrans.setResult(AppConstant._transResult_failure);
			regTransService.updateById(regTrans);
			smgpService.sendTxtSMS(regTrans.getCellphone(), "天翼手机深圳通开通失败，请稍后重试");
			return;
		}
		
		//调用深圳通接口
		byte[] deviceSequence = HexStringUtil.hexStringToBytes(regTrans.getDeviceSequence());
		byte[] sztSequence = HexStringUtil.hexStringToBytes(regTrans.getSztId());
		byte[] cardAppSequence = HexStringUtil.hexStringToBytes(regTrans.getCardAppSequence());
		byte[] regResult = HexStringUtil.hexStringToBytes("00000000");
		SZTRegResultReqTransVO sztRegResultRegTransVO = new SZTRegResultReqTransVO(deviceSequence,sztSequence,regTrans.getDeviceTriggerTime(),cardAppSequence,regResult);
		
		SZTRegResultRequestVO sztRegResultRequestVO = new  SZTRegResultRequestVO(sztRegResultRegTransVO.getTransDataHexString());
		
		SZTInterface sztInterface = new SZTInterface();
		byte[] response=sztInterface.sendRequest(HexStringUtil.hexStringToBytes(sztRegResultRequestVO.getMsgPackage()));
		SZTBaseResponseVO responseVO = new SZTBaseResponseVO();
		responseVO.parse(response);
		
		SZTRegResultResTransVO sztRegResultResTransVO = new SZTRegResultResTransVO();
		sztRegResultResTransVO.parse(HexStringUtil.hexStringToBytes(responseVO.getData()));
		
		String prompt;
		if (sztRegResultResTransVO.getTransResult()==0x00){//Success
			System.out.println("success");
			prompt = "天翼手机深圳通开通成功";
		}else{
			System.out.println("failure code="+HexStringUtil.byteToHexString(sztRegResultResTransVO.getTransResult()));
			prompt="天翼手机深圳通开通失败，请稍后重试";
		}
		smgpService.sendTxtSMS(regTrans.getCellphone(), prompt);
	}
	
	
	
}
